﻿Public Class frmPriceCalculation
    Dim dbBase As New dbPriceCalc
    Dim dbAdv As New dbAdvSource
    Dim frmCtrl As New FormControl
    Dim curr2word As New Currency
    Dim dsBase As DataSet
    Dim dsGroup As DataSet
    Dim dsItem As DataSet
    Dim dsPrincipal As DataSet
    Dim dsIncoterms As DataSet
    Dim dsRemarks As DataSet
    Dim dsUoM As DataSet
    Dim dsCurr As DataSet
    Dim Var As New varDeclare
    Dim TypeAct As String
    Dim inProcess As Boolean
    Public EntryID As String
    Dim dgvTemp As New DataGridView

    Private Sub initialize()
        '' ComboBox Currency
        dsCurr = New dbSystem().CurrList(Var.CompID)
        If (dsCurr.Tables(0).Rows.Count > 0) Then
            For i = 0 To dsCurr.Tables(0).Rows.Count - 1
                Try
                    cmbCurrBaseCurrency.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbCurrBuyingPrice.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbDlvCurrC1.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbDlvCurrC2.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbDlvCurrC3.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbDlvCurrC4.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                    cmbDlvCurrC5.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                Catch ex As Exception
                End Try
            Next
        End If

        '' ComboBox Status
        Dim cmbItem As New List(Of comboItem)
        cmbItem.Add(New comboItem("Active", "A"))
        cmbItem.Add(New comboItem("Inactive", "I"))
        cmbStatus.DataSource = cmbItem
        cmbStatus.DisplayMember = "Text"
        cmbStatus.ValueMember = "Value"

        '' Advance Search Text
        dsGroup = dbAdv.getGroupsForAdvList()
        AdvGroup.Initialize(dsGroup, "Code", txtGroups)
        dsItem = dbAdv.getItemCodeForAdvByGroups(txtGroups.Text.Trim())
        AdvItemCode.Initialize(dsItem, "Code", txtItemCode)
        dsPrincipal = dbAdv.GetPrincipalForAdv()
        AdvPrincipal.Initialize(dsPrincipal, "Code", txtPrincipalCode)
        dsIncoterms = dbAdv.GetIncotermForAdv()
        AdvIncoterms.Initialize(dsIncoterms, "Code", txtIncotermsCode)
        dsUoM = dbAdv.GetUoMForAdv()
        cmbUoM.DataSource = dsUoM.Tables(0)
        cmbUoM.DisplayMember = "Code"
        dsRemarks = dbAdv.GetRemarkCostForAdv()
        If (dsRemarks.Tables(0).Rows.Count > 0) Then
            For i = 0 To dsRemarks.Tables(0).Rows.Count - 1
                Try
                    cmbDlvRemarkC1.Items.Add(dsRemarks.Tables(0).Rows(i)("Name").ToString().Trim())
                    cmbDlvRemarkC2.Items.Add(dsRemarks.Tables(0).Rows(i)("Name").ToString().Trim())
                    cmbDlvRemarkC3.Items.Add(dsRemarks.Tables(0).Rows(i)("Name").ToString().Trim())
                    cmbDlvRemarkC4.Items.Add(dsRemarks.Tables(0).Rows(i)("Name").ToString().Trim())
                    cmbDlvRemarkC5.Items.Add(dsRemarks.Tables(0).Rows(i)("Name").ToString().Trim())
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Private Function toN4(txt As String) As String
        Return Convert.ToDecimal(txt).ToString("N4", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
    End Function

    Private Sub DoPriceCalculate()
        If (inProcess = False And txtGroups.Text <> "" And txtItemCode.Text <> "" And txtPrincipalCode.Text <> "" And cmbCurrBaseCurrency.Text <> "" And cmbCurrBuyingPrice.Text <> "" And txtBuyingPrice.Text <> "") Then
            inProcess = True
            Dim dtCalculate As DataTable = dbBase.DoPriceCalculation(cmbCurrBaseCurrency.Text, txtJobCurrSellPrice.Text, cmbCurrBuyingPrice.Text, txtBuyingPrice.Text, _
                                                                     cmbDlvCurrC1.Text, cmbDlvCurrC2.Text, cmbDlvCurrC3.Text, cmbDlvCurrC4.Text, "", txtDlvCurrExtCharg.Text, _
                                                                     txtDlvPercC1.Text, txtDlvPercC2.Text, txtDlvPercC3.Text, txtDlvPercC4.Text, "", txtDlvPercExtCharg.Text, _
                                                                     txtDlvC1.Text, txtDlvC2.Text, txtDlvC3.Text, txtDlvC4.Text, "", txtDlvExtCharg.Text, _
                                                                     txtJobF1.Text, txtDlvF.Text).Tables(0)
            txtDlvC1.Text = toN4(dtCalculate.Rows(0)("Cost1").ToString())
            txtDlvC2.Text = toN4(dtCalculate.Rows(0)("Cost2").ToString())
            txtDlvC3.Text = toN4(dtCalculate.Rows(0)("Cost3").ToString())
            txtDlvC4.Text = toN4(dtCalculate.Rows(0)("Cost4").ToString())
            txtDlvC5.Text = toN4(dtCalculate.Rows(0)("Cost5").ToString())
            txtDlvLandedC.Text = toN4(dtCalculate.Rows(0)("LandedCost").ToString())
            txtDlvExtCharg.Text = toN4(dtCalculate.Rows(0)("ExtraCharge").ToString())
            txtDlvSellPrice.Text = toN4(dtCalculate.Rows(0)("PriceDelivery").ToString())
            txtJobSellingPrice.Text = toN4(dtCalculate.Rows(0)("PriceJob").ToString())
            inProcess = False
        End If
    End Sub

    Private Sub frmPriceCalculation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgvTemp.Columns.Add("clID", "clID")
        dgvTemp.Rows.Add(EntryID)
        dgvTemp.Rows(0).Selected = True
        ToolbarHeader1.DefaultRow = 1
        ToolbarHeader1.Iniliaze("Entry : Price Calculation", Me, New frmPriceCalcList, dgvTemp, "clID")
        initialize()
        If (EntryID.Trim() <> "") Then
            btnClone.Visible = True
            inProcess = True
            Dim arr As Array = EntryID.Split("-")
            txtGroups.Text = arr(0).ToString().Trim()
            txtItemCode.Text = arr(1).ToString().Trim()
            dsBase = dbBase.GetItemDetail(EntryID)
            TypeAct = "U"
            txtGroups.Enabled = False
            txtGroupsDesc.Enabled = False
            txtItemCode.Enabled = False
            txtItemDesc.Enabled = False
            If (dsBase.Tables(0).Rows.Count > 0) Then
                txtPrincipalCode.Enabled = False
                txtPrincipalDesc.Enabled = False
                cmbUoM.Text = dsBase.Tables(0).Rows(0).Item("UOM").ToString().Trim()
                txtPrincipalCode.Text = dsBase.Tables(0).Rows(0).Item("principal_code").ToString().Trim()
                Dim dvIncoterm As DataView = New DataView(dsIncoterms.Tables(0))
                dvIncoterm.RowFilter = "[Name] = '" & dsBase.Tables(0).Rows(0).Item("INCOTERMS").ToString().Trim() & "'"
                txtIncotermsCode.Text = dvIncoterm.Item(0).Row("Code").ToString()

                txtLastUpdate.Text = Convert.ToDateTime(dsBase.Tables(0).Rows(0).Item("LAST_MODIFY")).ToString("dd MMM yyyy")
                txtLastEntryBy.Text = dsBase.Tables(0).Rows(0).Item("DATA_ENTRY").ToString().Trim()

                cmbCurrBaseCurrency.Text = dsBase.Tables(0).Rows(0).Item("CURR_BASE").ToString()
                cmbCurrBuyingPrice.Text = dsBase.Tables(0).Rows(0).Item("EXW_CURR").ToString()
                txtBuyingPrice.Text = dsBase.Tables(0).Rows(0).Item("EXWORKS").ToString()
                txtInterCompanyPrice.Text = dsBase.Tables(0).Rows(0).Item("INTCOMP").ToString()
                txtDlvC1.Text = dsBase.Tables(0).Rows(0).Item("JKT").ToString()
                txtDlvC2.Text = dsBase.Tables(0).Rows(0).Item("SIN").ToString()
                txtDlvC3.Text = dsBase.Tables(0).Rows(0).Item("OTHER_COST").ToString()
                txtDlvC4.Text = dsBase.Tables(0).Rows(0).Item("PR_COST4").ToString()
                cmbDlvCurrC1.Text = dsBase.Tables(0).Rows(0).Item("JKT_CURR").ToString()
                cmbDlvCurrC2.Text = dsBase.Tables(0).Rows(0).Item("SIN_CURR").ToString()
                cmbDlvCurrC3.Text = dsBase.Tables(0).Rows(0).Item("OTH_CURR").ToString()
                cmbDlvCurrC4.Text = dsBase.Tables(0).Rows(0).Item("PR_CURR4").ToString()
                txtDlvPercC1.Text = dsBase.Tables(0).Rows(0).Item("PERCENT_0").ToString()
                txtDlvPercC2.Text = dsBase.Tables(0).Rows(0).Item("PERCENT_1").ToString()
                txtDlvPercC3.Text = dsBase.Tables(0).Rows(0).Item("PERCENT_2").ToString()
                txtDlvPercC4.Text = dsBase.Tables(0).Rows(0).Item("PERCENT_3").ToString()
                txtDlvRemarkIdC1.Text = dsBase.Tables(0).Rows(0).Item("PR_CODE1").ToString()
                txtDlvRemarkIdC2.Text = dsBase.Tables(0).Rows(0).Item("PR_CODE2").ToString()
                txtDlvRemarkIdC3.Text = dsBase.Tables(0).Rows(0).Item("PR_CODE3").ToString()
                txtDlvRemarkIdC4.Text = dsBase.Tables(0).Rows(0).Item("PR_CODE4").ToString()
                txtDlvExtCharg.Text = dsBase.Tables(0).Rows(0).Item("CHG").ToString()
                txtDlvF.Text = dsBase.Tables(0).Rows(0).Item("F").ToString()
                txtJobF1.Text = dsBase.Tables(0).Rows(0).Item("F1").ToString()
                If (txtDlvPercC1.Text = "") Then
                    cmbDlvTypC1.Text = "Fix"
                Else : cmbDlvTypC1.Text = "%"
                End If
                If (txtDlvPercC2.Text = "") Then
                    cmbDlvTypC2.Text = "Fix"
                Else : cmbDlvTypC2.Text = "%"
                End If
                If (txtDlvPercC3.Text = "") Then
                    cmbDlvTypC3.Text = "Fix"
                Else : cmbDlvTypC3.Text = "%"
                End If
                If (txtDlvPercC4.Text = "") Then
                    cmbDlvTypC4.Text = "Fix"
                Else : cmbDlvTypC4.Text = "%"
                End If
                inProcess = False
                ToolbarHeader1.ActionCtrl(True, False, False, False, True, True, False, True, True)
                DoPriceCalculate()
            End If
        Else
            TypeAct = "I"
            btnClone.Visible = False
            ToolbarHeader1.ActionCtrl(False, False, False, False, False, True, False, False, True)
        End If
    End Sub

    ''================================== Control Header ==============================================''
    Private Sub tsNew_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsNewClick
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsEditClick
        
    End Sub

    Private Sub tsDelete_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            Dim result As String = "" 'dbGL.deleteJournalGL(txtJournalID.Text, Var.CompID)
            frmCtrl.openChild(New frmPriceCalcList, Me.ParentForm)
        Else : MessageBox.Show("Price Calculation is Not Exist")
        End If
    End Sub

    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        If (SaveValidation()) Then
            TypeAct = "I"
            Dim status As String = dbBase.PriceCalculationEntry(txtGroups.Text, txtItemCode.Text, txtPrincipalCode.Text, txtIncotermsDesc.Text, _
                                                            txtBuyingPrice.Text, cmbCurrBuyingPrice.Text, txtDlvRemarkIdC1.Text, txtDlvPercC1.Text, _
                                                            txtDlvC1.Text, cmbDlvCurrC1.Text, txtDlvRemarkIdC2.Text, txtDlvPercC2.Text, txtDlvC2.Text, _
                                                            cmbDlvCurrC2.Text, txtDlvRemarkIdC3.Text, txtDlvPercC3.Text, txtDlvC3.Text, cmbDlvCurrC3.Text, _
                                                            txtDlvRemarkIdC4.Text, txtDlvPercC4.Text, txtDlvC4.Text, cmbDlvCurrC4.Text, txtDlvRemarkIdC5.Text, _
                                                            txtDlvPercC5.Text, txtDlvC5.Text, cmbDlvCurrC5.Text, txtDlvLandedC.Text, txtDlvF.Text, txtJobF1.Text, _
                                                            txtDlvSellPrice.Text, txtJobSellingPrice.Text, cmbCurrBaseCurrency.Text, cmbUoM.Text, "", "", cmbStatus.SelectedValue.ToString(), _
                                                            txtPPICRemarks.Text, MDI.LoginUserName, txtDlvExtCharg.Text, txtDlvCurrExtCharg.Text, txtInterCompanyPrice.Text, TypeAct)
            If (status.IndexOf("Success") > 0) Then
                If (status.Length > 7) Then
                    MessageBox.Show(status)
                End If
                frmCtrl.openChild(New frmPriceCalcList, Me.ParentForm)
            Else
                MessageBox.Show("Saving Price Calculation is Failed")
            End If
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsRefreshClick
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsCancelClick
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub
    ''================================== Control Header [End] ==============================================''

    Private Sub ShowAdvGroups_DoubleClick(sender As Object, e As EventArgs) Handles txtGroups.DoubleClick, txtGroupsDesc.DoubleClick
        AdvGroup.Visible = True
        AdvGroup.BringToFront()
        AdvGroup._Show()
    End Sub

    Private Sub txtGroups_TextChanged(sender As Object, e As EventArgs) Handles txtGroups.TextChanged
        AdvGroup.Visible = False
        If (txtGroups.Text = "load") Then
            txtGroups.Text = AdvGroup.dsResult.Tables(0).Rows(0)("Code").ToString()
            txtGroupsDesc.Text = AdvGroup.dsResult.Tables(0).Rows(0)("Name").ToString()
            dsItem = dbAdv.getItemCodeForAdvByGroups(txtGroups.Text.Trim())
            AdvItemCode.Initialize(dsItem, "Code", txtItemCode)
        Else
            Dim dvGroups As DataView = New DataView(dsGroup.Tables(0))
            dvGroups.RowFilter = "[Code] = '" & txtGroups.Text.Trim() & "'"
            If (dvGroups.Count > 0) Then
                txtGroupsDesc.Text = dvGroups.Item(0).Row("Name").ToString()
                txtGroupsDesc.BackColor = Color.LightGray
                txtGroupsDesc.ReadOnly = True
                lblNewGroups.Visible = False
                dsItem = dbAdv.getItemCodeForAdvByGroups(txtGroups.Text.Trim())
                AdvItemCode.Initialize(dsItem, "Code", txtItemCode)
            Else
                txtGroupsDesc.Text = ""
                txtGroupsDesc.BackColor = Color.White
                txtGroupsDesc.ReadOnly = False
                lblNewGroups.Visible = True
                dsItem = dbAdv.getItemCodeForAdvByGroups("")
                AdvItemCode.Initialize(dsItem, "Code", txtItemCode)
            End If
            Dim txt As String
            txt = txtItemCode.Text
            txtItemCode.Text = ""
            txtItemCode.Text = txt
        End If
        If (txtGroups.Text = "") Then
            lblNewGroups.Visible = False
        End If
    End Sub

    Private Sub ShowAdvItemCode_DoubleClick(sender As Object, e As EventArgs) Handles txtItemCode.DoubleClick
        AdvItemCode.Visible = True
        AdvItemCode.BringToFront()
        AdvItemCode._Show()
    End Sub

    Private Sub txtItemCode_TextChanged(sender As Object, e As EventArgs) Handles txtItemCode.TextChanged
        AdvItemCode.Visible = False
        If (txtItemCode.Text = "load") Then
            Dim code As Array = AdvItemCode.dsResult.Tables(0).Rows(0)("Code").ToString().Split("-")
            txtItemCode.Text = code(1).ToString()
            txtItemDesc.Text = AdvItemCode.dsResult.Tables(0).Rows(0)("Name").ToString()
            txtGroups.Text = code(0).ToString()
        Else
            If (txtGroups.Text <> "") Then
                Dim dvItemCode As DataView = New DataView(dsItem.Tables(0))
                dvItemCode.RowFilter = "[Code] = '" & txtGroups.Text + "-" + txtItemCode.Text & "'"
                If (dvItemCode.Count > 0) Then
                    txtItemDesc.Text = dvItemCode.Item(0).Row("Name").ToString()
                    txtItemDesc.BackColor = Color.LightGray
                    txtItemDesc.ReadOnly = True
                    lblNewItemCode.Visible = False
                Else
                    txtItemDesc.Text = ""
                    txtItemDesc.BackColor = Color.White
                    txtItemDesc.ReadOnly = False
                    lblNewItemCode.Visible = True
                End If
            End If
        End If
    End Sub

    Private Sub ShowAdvPrincipal_DoubleClick(sender As Object, e As EventArgs) Handles txtPrincipalCode.DoubleClick, txtPrincipalDesc.DoubleClick
        AdvPrincipal.Visible = True
        AdvPrincipal.BringToFront()
        AdvPrincipal._Show()
    End Sub

    Private Sub txtPrincipalCode_TextChanged(sender As Object, e As EventArgs) Handles txtPrincipalCode.TextChanged
        AdvPrincipal.Visible = False
        If (txtPrincipalCode.Text = "load") Then
            txtPrincipalCode.Text = AdvPrincipal.dsResult.Tables(0).Rows(0)("Code").ToString()
            txtPrincipalCode.Text = AdvPrincipal.dsResult.Tables(0).Rows(0)("Name").ToString()
        Else
            If (txtGroups.Text <> "") Then
                Dim dvPrincipal As DataView = New DataView(dsPrincipal.Tables(0))
                dvPrincipal.RowFilter = "[Code] = '" & txtPrincipalCode.Text & "'"
                txtPrincipalDesc.Text = dvPrincipal.Item(0).Row("Name").ToString()
            End If
        End If
    End Sub

    Private Sub ShowAdvIncoterms_DoubleClick(sender As Object, e As EventArgs) Handles txtIncotermsCode.DoubleClick, txtIncotermsDesc.DoubleClick
        AdvIncoterms.Visible = True
        AdvIncoterms.BringToFront()
        AdvIncoterms._Show()
    End Sub

    Private Sub txtIncotermsCode_TextChanged(sender As Object, e As EventArgs) Handles txtIncotermsCode.TextChanged
        AdvPrincipal.Visible = False
        If (txtIncotermsCode.Text = "load") Then
            txtIncotermsCode.Text = AdvIncoterms.dsResult.Tables(0).Rows(0)("Code").ToString()
            txtIncotermsCode.Text = AdvIncoterms.dsResult.Tables(0).Rows(0)("Name").ToString()
        Else
            If (txtGroups.Text <> "") Then
                Dim dvIncoterm As DataView = New DataView(dsIncoterms.Tables(0))
                dvIncoterm.RowFilter = "[Code] = '" & txtIncotermsCode.Text & "'"
                txtIncotermsDesc.Text = dvIncoterm.Item(0).Row("Name").ToString()
            End If
        End If
    End Sub

    Private Sub txtN4_Leave(sender As Object, e As EventArgs) Handles txtBuyingPrice.Leave, txtInterCompanyPrice.Leave, txtDlvSellPrice.Leave, txtDlvLandedC.Leave, txtDlvExtCharg.Leave, txtDlvC1.Leave, txtDlvC2.Leave, txtDlvC3.Leave, txtDlvC4.Leave, txtDlvC5.Leave, txtJobExtraCharge.Leave, txtJobSellingPrice.Leave, txtDlvActLandedC.Leave, txtDlvDiff.Leave
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = toN4(txt.Text) ' Convert.ToDecimal(txt.Text).ToString("N4", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        End If
    End Sub

    Private Sub txtN2_Leave(sender As Object, e As EventArgs) Handles txtDlvPercExtCharg.Leave, txtDlvPercC1.Leave, txtDlvPercC2.Leave, txtDlvPercC3.Leave, txtDlvPercC4.Leave, txtDlvPercC5.Leave, txtDlvF.Leave, txtJobF1.Leave
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = curr2word.dec2curr(txt.Text)
        End If
    End Sub

    Private Sub txtCurr_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtBuyingPrice.Validating, txtInterCompanyPrice.Validating, txtDlvSellPrice.Validating, txtDlvPercExtCharg.Validating, txtDlvLandedC.Validating, txtDlvF.Validating, txtDlvExtCharg.Validating, txtDlvPercC1.Validating, txtDlvPercC2.Validating, txtDlvPercC3.Validating, txtDlvPercC4.Validating, txtDlvPercC5.Validating, txtDlvC1.Validating, txtDlvC2.Validating, txtDlvC3.Validating, txtDlvC4.Validating, txtDlvC5.Validating, txtDlvRemarkIdC1.Validating, txtDlvRemarkIdC2.Validating, txtDlvRemarkIdC3.Validating, txtDlvRemarkIdC4.Validating, txtDlvRemarkIdC5.Validating, txtJobF1.Validating, txtJobExtraCharge.Validating, txtJobSellingPrice.Validating, txtDlvActLandedC.Validating, txtDlvDiff.Validating
        Dim txt As TextBox = sender
        If (txt.Text.Trim() <> "") Then
            If (IsNumeric(txt.Text) = False) Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtCurr_Enter(sender As Object, e As EventArgs) Handles txtBuyingPrice.Enter, txtInterCompanyPrice.Enter, txtDlvSellPrice.Enter, txtDlvPercExtCharg.Enter, txtDlvLandedC.Enter, txtDlvF.Enter, txtDlvExtCharg.Enter, txtDlvC1.Enter, txtDlvPercC1.Enter, txtDlvC4.Enter, txtDlvPercC4.Enter, txtDlvC5.Enter, txtDlvPercC5.Enter, txtDlvC3.Enter, txtDlvPercC3.Enter, txtDlvC2.Enter, txtDlvPercC2.Enter, txtJobF1.Enter, txtJobExtraCharge.Enter, txtJobSellingPrice.Enter, txtDlvActLandedC.Enter, txtDlvDiff.Enter
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = Convert.ToDecimal(txt.Text).ToString()
            BeginInvoke(DirectCast(Sub() txt.SelectAll(), Action))
        End If
    End Sub

    Private Sub txtCurr_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtBuyingPrice.KeyPress, txtInterCompanyPrice.KeyPress, txtDlvSellPrice.KeyPress, txtDlvPercExtCharg.KeyPress, txtDlvLandedC.KeyPress, txtDlvF.KeyPress, txtDlvExtCharg.KeyPress, txtDlvPercC1.KeyPress, txtDlvPercC2.KeyPress, txtDlvPercC3.KeyPress, txtDlvPercC4.KeyPress, txtDlvPercC5.KeyPress, txtDlvC1.KeyPress, txtDlvC2.KeyPress, txtDlvC3.KeyPress, txtDlvC4.KeyPress, txtDlvC5.KeyPress, txtDlvRemarkIdC1.KeyPress, txtDlvRemarkIdC2.KeyPress, txtDlvRemarkIdC3.KeyPress, txtDlvRemarkIdC4.KeyPress, txtDlvRemarkIdC5.KeyPress, txtJobF1.KeyPress, txtJobExtraCharge.KeyPress, txtJobSellingPrice.KeyPress, txtDlvActLandedC.KeyPress, txtDlvDiff.KeyPress
        Dim txt As TextBox = sender
        Dim FullStop As Char
        FullStop = "."
        If e.KeyChar = FullStop And txt.Text.IndexOf(FullStop) <> -1 Then
            e.Handled = True
            Return
        End If
        If Not Char.IsDigit(e.KeyChar) Then
            If (e.KeyChar <> FullStop) And
               (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub

    Private Sub cmbCurrBaseCurrency_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCurrBaseCurrency.SelectedIndexChanged
        If (cmbCurrBaseCurrency.SelectedItem.ToString() <> "") Then
            Dim dvCurr As DataView = New DataView(dsCurr.Tables(0))
            dvCurr.RowFilter = "[ID] = '" & cmbCurrBaseCurrency.SelectedItem.ToString() & "'"
            txtBaseCurency.Text = dvCurr.Item(0).Row("Name").ToString()
        End If
        txtDlvCurrLandedC.Text = cmbCurrBaseCurrency.Text
        txtDlvCurrSellPrice.Text = cmbCurrBaseCurrency.Text
        DoPriceCalculate()
    End Sub

    Private Sub cmbCurrBuyingPrice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCurrBuyingPrice.SelectedIndexChanged
        txtJobCurrSellPrice.Text = cmbCurrBuyingPrice.Text
        txtDlvCurrExtCharg.Text = cmbCurrBuyingPrice.Text
        txtJobCurrExtCharg.Text = cmbCurrBuyingPrice.Text
        txtCurrInterCompPrice.Text = cmbCurrBuyingPrice.Text
        DoPriceCalculate()
    End Sub

    Private Sub cmb_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cmbDlvCurrC1.Validating, cmbDlvCurrC2.Validating, cmbDlvCurrC3.Validating, cmbDlvCurrC4.Validating, cmbCurrBaseCurrency.Validating, cmbCurrBuyingPrice.Validating
        Dim cmb As ComboBox = sender
        If (cmb.Text <> "") Then
            If (cmb.SelectedIndex < 0) Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtDlvExtCharg_TextChanged(sender As Object, e As EventArgs) Handles txtDlvExtCharg.TextChanged
        txtJobExtraCharge.Text = txtDlvExtCharg.Text
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercC1_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercC1.TextChanged
        If (txtDlvPercC1.Text = "") Then
            cmbDlvTypC1.Text = "Fix"
            txtDlvC1.ReadOnly = False
        Else
            cmbDlvTypC1.Text = "%"
            txtDlvC1.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercC2_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercC2.TextChanged
        If (txtDlvPercC2.Text = "") Then
            cmbDlvTypC2.Text = "Fix"
            txtDlvC2.ReadOnly = False
        Else
            cmbDlvTypC2.Text = "%"
            txtDlvC2.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercC3_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercC3.TextChanged
        If (txtDlvPercC3.Text = "") Then
            cmbDlvTypC3.Text = "Fix"
            txtDlvC3.ReadOnly = False
        Else
            cmbDlvTypC3.Text = "%"
            txtDlvC3.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercC4_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercC4.TextChanged
        If (txtDlvPercC4.Text = "") Then
            cmbDlvTypC4.Text = "Fix"
            txtDlvC4.ReadOnly = False
        Else
            cmbDlvTypC4.Text = "%"
            txtDlvC4.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercC5_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercC5.TextChanged
        If (txtDlvPercC5.Text = "") Then
            cmbDlvTypC5.Text = "Fix"
            txtDlvC5.ReadOnly = False
        Else
            cmbDlvTypC5.Text = "%"
            txtDlvC5.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvPercExtCharg_TextChanged(sender As Object, e As EventArgs) Handles txtDlvPercExtCharg.TextChanged
        If (txtDlvPercExtCharg.Text = "") Then
            cmbDlvTypExtCharg.Text = "Fix"
            txtDlvExtCharg.ReadOnly = False
        Else
            cmbDlvTypExtCharg.Text = "%"
            txtDlvExtCharg.ReadOnly = True
        End If
        DoPriceCalculate()
    End Sub

    Private Sub txtDlvRemarkIdC1_TextChanged(sender As Object, e As EventArgs) Handles txtDlvRemarkIdC1.TextChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Code] = '" & txtDlvRemarkIdC1.Text & "'"
        If (dvRemarks.Count > 0) Then
            cmbDlvRemarkC1.Text = dvRemarks.Item(0).Row("Name").ToString()
        Else : cmbDlvRemarkC1.Text = ""
        End If
    End Sub

    Private Sub txtDlvRemarkIdC2_TextChanged(sender As Object, e As EventArgs) Handles txtDlvRemarkIdC2.TextChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Code] = '" & txtDlvRemarkIdC2.Text & "'"
        If (dvRemarks.Count > 0) Then
            cmbDlvRemarkC2.Text = dvRemarks.Item(0).Row("Name").ToString()
        Else : cmbDlvRemarkC2.Text = ""
        End If
    End Sub

    Private Sub txtDlvRemarkIdC3_TextChanged(sender As Object, e As EventArgs) Handles txtDlvRemarkIdC3.TextChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Code] = '" & txtDlvRemarkIdC3.Text & "'"
        If (dvRemarks.Count > 0) Then
            cmbDlvRemarkC3.Text = dvRemarks.Item(0).Row("Name").ToString()
        Else : cmbDlvRemarkC3.Text = ""
        End If
    End Sub

    Private Sub txtDlvRemarkIdC4_TextChanged(sender As Object, e As EventArgs) Handles txtDlvRemarkIdC4.TextChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Code] = '" & txtDlvRemarkIdC4.Text & "'"
        If (dvRemarks.Count > 0) Then
            cmbDlvRemarkC4.Text = dvRemarks.Item(0).Row("Name").ToString()
        Else : cmbDlvRemarkC4.Text = ""
        End If
    End Sub

    Private Sub txtDlvRemarkIdC5_TextChanged(sender As Object, e As EventArgs) Handles txtDlvRemarkIdC5.TextChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Code] = '" & txtDlvRemarkIdC5.Text & "'"
        If (dvRemarks.Count > 0) Then
            cmbDlvRemarkC5.Text = dvRemarks.Item(0).Row("Name").ToString()
        Else : cmbDlvRemarkC5.Text = ""
        End If
    End Sub

    Private Sub cmbDlvRemarkC1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvRemarkC1.SelectedIndexChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Name] = '" & cmbDlvRemarkC1.Text & "'"
        If (dvRemarks.Count > 0) Then
            txtDlvRemarkIdC1.Text = dvRemarks.Item(0).Row("Code").ToString()
        Else : txtDlvRemarkIdC1.Text = ""
        End If
    End Sub

    Private Sub cmbDlvRemarkC2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvRemarkC2.SelectedIndexChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Name] = '" & cmbDlvRemarkC2.Text & "'"
        If (dvRemarks.Count > 0) Then
            txtDlvRemarkIdC2.Text = dvRemarks.Item(0).Row("Code").ToString()
        Else : txtDlvRemarkIdC2.Text = ""
        End If
    End Sub

    Private Sub cmbDlvRemarkC3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvRemarkC3.SelectedIndexChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Name] = '" & cmbDlvRemarkC3.Text & "'"
        If (dvRemarks.Count > 0) Then
            txtDlvRemarkIdC3.Text = dvRemarks.Item(0).Row("Code").ToString()
        Else : txtDlvRemarkIdC3.Text = ""
        End If
    End Sub

    Private Sub cmbDlvRemarkC4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvRemarkC4.SelectedIndexChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Name] = '" & cmbDlvRemarkC4.Text & "'"
        If (dvRemarks.Count > 0) Then
            txtDlvRemarkIdC4.Text = dvRemarks.Item(0).Row("Code").ToString()
        Else : txtDlvRemarkIdC4.Text = ""
        End If
    End Sub

    Private Sub cmbDlvRemarkC5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvRemarkC5.SelectedIndexChanged
        Dim dvRemarks As DataView = New DataView(dsRemarks.Tables(0))
        dvRemarks.RowFilter = "[Name] = '" & cmbDlvRemarkC5.Text & "'"
        If (dvRemarks.Count > 0) Then
            txtDlvRemarkIdC5.Text = dvRemarks.Item(0).Row("Code").ToString()
        Else : txtDlvRemarkIdC5.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypC1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypC1.SelectedIndexChanged
        If (cmbDlvTypC1.Text = "Fix") Then
            txtDlvPercC1.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypC2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypC2.SelectedIndexChanged
        If (cmbDlvTypC2.Text = "Fix") Then
            txtDlvPercC2.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypC3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypC3.SelectedIndexChanged
        If (cmbDlvTypC3.Text = "Fix") Then
            txtDlvPercC3.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypC4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypC4.SelectedIndexChanged
        If (cmbDlvTypC4.Text = "Fix") Then
            txtDlvPercC4.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypC5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypC5.SelectedIndexChanged
        If (cmbDlvTypC5.Text = "Fix") Then
            txtDlvPercC5.Text = ""
        End If
    End Sub

    Private Sub cmbDlvTypExtCharg_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvTypExtCharg.SelectedIndexChanged
        If (cmbDlvTypExtCharg.Text = "Fix") Then
            txtDlvPercExtCharg.Text = ""
        End If
    End Sub

    Private Sub txtDoCalc_TextChanged(sender As Object, e As EventArgs) Handles txtBuyingPrice.TextChanged, txtDlvC1.TextChanged, txtDlvC2.TextChanged, txtDlvC3.TextChanged, txtDlvC4.TextChanged, txtDlvC5.TextChanged, txtDlvF.TextChanged, txtJobF1.TextAlignChanged
        DoPriceCalculate()
    End Sub

    Private Sub cmbDoCalc_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDlvCurrC1.SelectedIndexChanged, cmbDlvCurrC2.SelectedIndexChanged, cmbDlvCurrC3.SelectedIndexChanged, cmbDlvCurrC4.SelectedIndexChanged, cmbDlvCurrC5.SelectedIndexChanged
        DoPriceCalculate()
    End Sub

    Private Sub btnClone_Click(sender As Object, e As EventArgs) Handles btnClone.Click
        btnClone.Visible = False
        ToolbarHeader1.ActionCtrl(False, False, False, False, False, True, False, False, True)
        txtLastUpdate.Text = DateTime.Now.ToString("dd MMM yyyy")
        txtLastEntryBy.Text = MDI.LoginUserName
        TypeAct = "C"
        EntryID = ""
        txtGroups.Enabled = True
        txtGroupsDesc.Enabled = True
        txtItemCode.Enabled = True
        txtItemDesc.Enabled = True
        txtPrincipalCode.Enabled = True
        txtPrincipalDesc.Enabled = True
        txtGroups.Text = ""
        txtGroupsDesc.Text = ""
        txtItemCode.Text = ""
        txtItemDesc.Text = ""
        cmbStatus.Text = "Active"
    End Sub

    Private Function SaveValidation() As Boolean
        Dim status As Boolean = False
        Select Case TypeAct
            Case "D"
                If (EntryID <> "") Then
                    status = True
                End If
            Case "I"

            Case "U"

            Case "C"

        End Select
        Return status
    End Function
End Class